import type { ElementHiderRuleConfig } from '../../../types/constants'
import { PageHandler } from '../../../lib/page_handler'
import { BingPageType } from '../../../lib/page_handler/platform/bing/types'
import { ElementSelectorPosition, ElementSelectorType } from '../../../types/constants'
import {
  ELEMENT_IDENTIFIER_CONFIG,
} from '../../service/element_identifier/types'
import { addPrefixToConfig } from '../element_config'

export const BING_WEB_PAGE_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.default.regionSwitch.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_header #est_switch',
    position: ElementSelectorPosition.TopLeft,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
        BingPageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.searchTab.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_header nav[role=\'navigation\']',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
        BingPageType.Video,
        BingPageType.Academic,
        BingPageType.Dictionary,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.unsupportedSearchTab.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: '#b_header nav[role=\'navigation\'] li:is(#b-scopeListItem-images, #b-scopeListItem-local, #b-scopeListItem-menu)',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
        BingPageType.Video,
        BingPageType.Academic,
        BingPageType.Dictionary,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.userCenter.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_header #id_h',
    position: ElementSelectorPosition.TopRight,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
        BingPageType.Video,
        BingPageType.Academic,
        BingPageType.Dictionary,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.searchCount.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content #b_tween #b_tween_searchResults',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.filterComponent.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content #b_tween #b_tween_searchTools',
    nameZh: '内容筛选器',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.summary.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content #b_topw',
    nameZh: '结果摘要',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.metadata.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '#b_content #b_results > li .b_tpcn',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.everyoneSearching.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '#b_content #b_results > li.b_ans',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.searchPagination.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: '#b_content #b_results > .b_pag, #sentenceSeg > .b_pag',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
        BingPageType.Dictionary,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.selectedSearch.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#qs_searchBoxOuter',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
        BingPageType.Academic,
        BingPageType.Dictionary,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.rightAside.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content aside',
    position: ElementSelectorPosition.MiddleRight,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.footer.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#b_footer',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Web,
        BingPageType.Video,
        BingPageType.Dictionary,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
}

export const BING_VIDEO_PAGE_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.component.filterComponent.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_header nav[role=\'navigation\'] #fltIdtCon',
    nameZh: '内容筛选器',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.moreButton.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content #vm_res .btn_seemore',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.moreVideo.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content #vm_res .vsb_canvas.isvsr',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.feedback.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#fbpgbt',
    position: ElementSelectorPosition.BottomRight,
    enable: () => {
      const supportPages = [
        BingPageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
}

export const BING_ACADEMIC_PAGE_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.search.searchCount.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '#b_content #b_results > li.aca_algo_count',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Academic,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.searchPagination.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: '#b_content #b_results > .b_pag, #sentenceSeg > .b_pag',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Academic,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.orderComponent.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content .aca_topBar',
    nameZh: '内容排序器',
    position: ElementSelectorPosition.MiddleRight,
    enable: () => {
      const supportPages = [
        BingPageType.Academic,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.filterComponent.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content .aca_filterBar',
    nameZh: '内容筛选器',
    position: ElementSelectorPosition.MiddleRight,
    enable: () => {
      const supportPages = [
        BingPageType.Academic,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.tipMessage.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#b_content .aca_contact',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Academic,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.footer.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#b_footer',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Academic,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
}

export const BING_INDEX_PAGE_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.search.searchTab.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#hp_app header.header .head_cont',
    position: ElementSelectorPosition.TopLeft,
    enable: () => {
      const supportPages = [
        BingPageType.Index,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.userCenter.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#hp_app header.header .identity_wrapper',
    position: ElementSelectorPosition.TopRight,
    enable: () => {
      const supportPages = [
        BingPageType.Index,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.regionSwitch.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#hp_app #est_switch',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Index,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.hotNews.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: `#hp_app .below_sbox #tobPrompt`,
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Index,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.discovery.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: `#hp_app .hp_body > .hpl:has(#vs_cont)`,
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        BingPageType.Index,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as BingPageType)
    },
  },
}

/**
 * 所有配置
 */
export const BING_WEB_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  ...addPrefixToConfig(BING_WEB_PAGE_ELEMENT_CONFIG, 'web'),
  ...addPrefixToConfig(BING_VIDEO_PAGE_ELEMENT_CONFIG, 'video'),
  ...addPrefixToConfig(BING_ACADEMIC_PAGE_ELEMENT_CONFIG, 'academic'),
  ...addPrefixToConfig(BING_INDEX_PAGE_ELEMENT_CONFIG, 'index'),
}
